﻿#region

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using BusinessObjects;
using DataAccessLayer;

#endregion

namespace BusinessLogicLayer
{
    public static class AdministratorBLL
    {
        /// <summary>
        ///     Register a administrator
        /// </summary>
        /// <param name="staffId">Represent's the staff id</param>
        /// <param name="username">Represent's the username</param>
        /// <param name="password">Represent's the password</param>
        /// <param name="role">Represent's the role</param>
        public static void RegisterAdmin(int staffId, string username, string password, string role)
        {
            var userCredentail = new User
            {
                Username = username.Trim(),
                Password = EncryptPassword(password)
            };

            var admin = new Administrator
            {
                StaffId = staffId,
                Role = role,
                UserCredentials = userCredentail
            };

            AdministratorDAL.RegisterAdmin(admin);
        }

        /// <summary>
        ///     Encrypting the password using SHA1 hashing technique
        /// </summary>
        /// <param name="password">Represent's the password to be encrypted</param>
        /// <returns>Encrypted password</returns>
        private static string EncryptPassword(string password)
        {
            var sha1 = new SHA1CryptoServiceProvider();
            var utf8 = new UTF8Encoding();
            var bytes = sha1.ComputeHash(utf8.GetBytes(password));
            return BitConverter.ToString(bytes).Replace("-", "");
        }

        /// <summary>
        ///     Get All administrators
        /// </summary>
        /// <returns>All administrators</returns>
        public static List<Administrator> GetAllAdministrators()
        {
            return AdministratorDAL.GetAllAdministrators();
        }

        /// <summary>
        ///     Get administrator by staff Id
        /// </summary>
        /// <param name="staffId">Represent's the staff id</param>
        /// <returns>Administrator</returns>
        public static Administrator GetAdministrator(int staffId)
        {
            return AdministratorDAL.GetAdministrator(staffId);
        }

        /// <summary>
        ///     Get administrator by username
        /// </summary>
        /// <param name="username">Represent's the administrator's username</param>
        /// <returns>Administrator</returns>
        public static Administrator GetAdministrator(string username)
        {
            return AdministratorDAL.GetAdministrator(username);
        }
    }
}